"""
---------------------------------------------------------------------
- File: router.py
- Date: 2025/9/19
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description:
---------------------------------------------------------------------
"""

from typing import List, Union
from fastapi import APIRouter, Depends

# dependencies
# from app.api.v1.dependencies.common import get_current_user
from app.common.app_response import UnifiedResponse
from app.api.v1.endpoints.system_manage.user.schemas import (
    UserIn,
    UserListOut,
    UserPwdIn,
)
from .service import UserService

user_service = UserService()


router = APIRouter(
    prefix="/system_manage/user",
    tags=["系统用户管理"],
    # dependencies=[Depends(get_current_user)],
)


@router.post(
    "/add_user",
    summary="新增用户",
    response_model=UnifiedResponse[UserListOut],
)
async def add_user(user_in: UserIn):
    """新增用户"""
    new_user = await user_service.add_user(user_in)
    # 保存用户角色信息
    return UnifiedResponse(data=new_user, msg="新增用户成功")


@router.delete(
    "/delete_user/{user_id}",
    summary="删除用户",
    response_model=UnifiedResponse[dict],
)
async def delete_user(user_id: int):
    """删除用户"""
    result = await user_service.delete_user(user_id)
    if result is None:
        return UnifiedResponse(code=201, msg="用户不存在", data={})
    else:
        return UnifiedResponse(data=result, msg="删除用户成功")


@router.post(
    "/delete_batch_user", summary="批量删除用户", response_model=UnifiedResponse[dict]
)
async def delete_batch_user(user_ids: List[int | str]):
    """批量删除用户"""
    print("收到的参数：", user_ids)
    result = await user_service.batch_delete_user(user_ids)
    return UnifiedResponse(data=result, msg="批量删除用户成功")


@router.put(
    "/update_user/{user_id}",
    summary="更新用户",
    response_model=UnifiedResponse[Union[UserListOut, dict]],
)
async def update_user(user_id: int, user_in: UserIn):
    """更新用户"""
    updated_user = await user_service.update_user(user_id, user_in)
    if updated_user is None:
        return UnifiedResponse(code=201, msg="用户不存在", data={})
    return UnifiedResponse(data=updated_user, msg="更新用户成功")


@router.get(
    "/get_user/{user_id}",
    summary="获取用户信息",
    response_model=UnifiedResponse[Union[UserListOut, dict]],
)
async def get_user(user_id: int):
    """获取用户信息"""
    user = await user_service.get_user(user_id)
    if user is None:
        return UnifiedResponse(code=201, msg="用户不存在", data={})
    return UnifiedResponse(data=user, msg="获取用户信息成功")


@router.get(
    "/get_user_list",
    summary="获取用户列表",
    response_model=UnifiedResponse[List[UserListOut]],
)
async def get_user_list():
    """获取用户列表"""
    user_list = await user_service.get_user_list(page=1, page_size=10)
    return UnifiedResponse(data=user_list)


@router.put(
    "/update_user_password/{user_id}",
    summary="更新用户密码",
    response_model=UnifiedResponse[Union[UserListOut, dict]],
)
async def update_user_password(user_id: int, user_pwd: UserPwdIn):
    """更新用户密码"""
    print("收到的参数：", user_id, user_pwd)
    result = await user_service.update_user_password(user_id, user_pwd)
    if result is None:
        return UnifiedResponse(code=201, msg="用户不存在", data={})
    return UnifiedResponse(data=result, msg="更新用户密码成功")
